var cheerio = require('cheerio');
var fs = require('fs');
var lib = require('../../lib/index.js');
var mysql = require('mysql');

// 1.创建数据链接
var connection = lib.connection();
connection.on('error', function(err) {
    console.log('db error', err);
    if (err.code === 'PROTOCOL_CONNECTION_LOST') {
        connection = lib.connection();
    } else {
        throw err;
    }
});

// let url = 'https://www.made-in-china.com/prod/catlist/';
let url = 'https://www.made-in-china.com/suppliers-discovery/';

// let page = 1;
async function getDetailCategory(link,categoryIndex) {
    let detailResult = await lib.myRequest(link);
    let $ = cheerio.load(detailResult);

    // if(categoryIndex==0){
        let childCategory = [];
        $('.list-group .section').each(function (index) {
            $(this).find('.list-cat a').each(function () {
                let href = 'https:' + $(this).attr('href');
                let text = $(this).text();

                // console.log(text);
                // console.log(href);
                childCategory.push({
                    href:href,
                    text:text,
                });
            });
        });

        fs.writeFile('./category/'+categoryIndex+'.json', JSON.stringify(childCategory), function(err) {
            if (err) console.log('写文件操作失败');
            else console.log('写文件操作成功');
        });
    // }
}
async function getCategory() {
    let result = await lib.myRequest(url);
    let $ = cheerio.load(result);

    $('.cat-list').each(function (index) {
        let href = 'https:' + $(this).find('.more a').attr('href');

        // setTimeout(function () {
            getDetailCategory(href,index);
        //     // index <1 && getDetailCategory(href)
        // }, 500*index);
        // console.log(href);
    });
    return false;
}
getCategory();
